package com.aqgj.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by Jetvae on 2017/6/22.
 */
public class GenericsUtils{
    private static final Log log = LogFactory.getLog(GenericsUtils.class);
    private static final String and = " and ";
    private static final String or = " or ";

    public static Class getSuperClassGenricType(Class clazz)
    {
        return getSuperClassGenricType(clazz, 0);
    }

    public static Class getSuperClassGenricType(Class clazz, int index)
    {
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            return Object.class;
        }
        Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
        if ((index >= params.length) || (index < 0))
        {
            log.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " +
                    params.length);
            return Object.class;
        }
        if (!(params[index] instanceof Class))
        {
            log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
            return Object.class;
        }
        return (Class)params[index];
    }

    public static boolean isEmpty(String string){
        if(string==null||string.isEmpty()){
            return true;
        }
        if(string.trim().length()==0){
            return true;
        }
        return false;
    }
}
